﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Equation.Enterprise.Framework.Web.Model;

using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using ENail.ApplicationServices;
using ENail.Portal.Web.Models;


namespace ENail.Portal.Web.Controllers
{
    public class BillingController : BaseController
    {
        private IRegistrationService _service;
        private ListViewModelMapper<RegistrationListViewItem, Registration> _listMapper;
        private ViewModelMapper<ReceiptViewModel, Receipt> _mapper;
        private readonly ITaskService _taskService;
        private TaskManager _taskManager;

        public BillingController(IRegistrationService service,ITaskService taskService)
        {
            _service = service;
            _listMapper = new RegistrationListViewModelMapper();
            _mapper = new ReceiptViewModelMapper();
            _taskService = taskService;
            _taskManager = new TaskManager(_taskService);

        }

        public ActionResult List(int? page, string filter)
        {
            List<Registration> domainModel = GetList();
            GenericListViewModel<RegistrationListViewItem> viewModel = new GenericListViewModel<RegistrationListViewItem>();
            viewModel.Page = page;
            viewModel.Filter = filter;

            _listMapper.MapDomainToView(domainModel, viewModel);

            return View("BillingList", viewModel);
        }

        [HttpPost]
        public ActionResult List(GenericListViewModel<RegistrationListViewItem> viewModel)
        {
            return List(null, viewModel.Filter);
        }

        public ActionResult Update(int id)
        {
            Receipt domainModel = Get(id);
            ReceiptViewModel viewModel = new ReceiptViewModel();
            _mapper.MapDomainToView(domainModel, viewModel);

            return View("Billing", viewModel);
        }

        [HttpPost]
        public ActionResult Update(int id, ReceiptViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                _mapper.ReMap(viewModel);
                return View("Billing", viewModel);
            }

            Receipt domainModel = Get(id);
            _mapper.MapViewToDomain(viewModel, domainModel);
            Update(domainModel);

            return RedirectToAction("Update", new { id = id });
        }

        //[HttpPost,HttpGet]
        public ActionResult Close(int id)
        {
            Receipt domainModel = Get(id);
            Close(domainModel);

            return RedirectToAction("List");
        }

        private List<Registration> GetList()
        {
            //return _service.GetAllActiveRegistrations().ToList();
            return _service.GetAllRegistrations().ToList();
        }

        private Receipt Get(int id)
        {
            Registration registration = _service.GetRegistration(id);
            if (registration.Receipt == null)
                registration.Receipt = new Receipt { Registration = registration };

            registration.Receipt.updateReceipt();

            return registration.Receipt;
        }

        private void Update(Receipt domainModel)
        {
            _service.UpdateRegistration(domainModel.Registration);
        }
        /// <summary>
        /// Payment and print receipt
        /// </summary>
        /// <param name="domainModel"></param>
        private void Close(Receipt domainModel)
        {
            domainModel.Registration.Close();
            _service.UpdateRegistration(domainModel.Registration);
        }
        #region View Task
        [ActionName("InitializeTask")]
        [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post | HttpVerbs.Head)]

        public ActionResult InitializeTask()
        {
            return PartialView("_Task", _taskManager.GetTaskModel());
        }
        #endregion

    }
}
